package com.whately.core.redis.controller;

import com.whately.core.base.annotation.IgnoreAccessKey;
import com.whately.core.base.annotation.IgnoreSession;
import com.whately.core.base.constant.WhatelyCoreCacheKeyEnum;
import com.whately.core.base.model.ApiResponse;
import com.whately.core.base.model.RedisKeyBuilder;
import com.whately.core.base.selfconfig.SelfDefineConfigRbacSuperAdmin;
import com.whately.core.base.selfconfig.SelfDefineConfigWhLog;
import com.whately.core.base.utils.IpUtils;
import com.whately.core.redis.utils.RedisCacheStringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 后端参数刷新
 */
@RestController
@RequestMapping("/api")
@Slf4j
public class WhatelyConfigRefreshController {
    private final String REFRESH_TOKEN = "64b880d0466d4a52b2c9b4bd99c4989c";


    @Autowired
    private RedisCacheStringUtils redisCacheStringUtils;

    @Autowired
    private SelfDefineConfigWhLog selfDefineConfigWhLog;

    @Autowired
    private SelfDefineConfigRbacSuperAdmin selfDefineConfigRbacSuperAdmin;
    /*
     *   配置信息
     */
    @GetMapping(params = "method=whately.core.config.refresh")
    @IgnoreSession
    @IgnoreAccessKey
    public ApiResponse refreshConfig(HttpServletRequest httpServletRequest,@RequestParam String token ,@RequestParam  String key) {


        String remoteIp = IpUtils.getRemoteClientIp(httpServletRequest);

        if(!IpUtils.isInternal(remoteIp)){
            return ApiResponse.builder().code(400).message("不支持的ip").build();
        }

        if(!REFRESH_TOKEN.equals(token)){
            return ApiResponse.builder().code(400).message("不支持的token").build();
        }

        if(key.equals("whlog")){
            resetWhValue();
        }else if(key.equals("superadmin")){
            resetSuperAdminConfig();
        }

        Map<String ,Object> map = new HashMap<>();
        map.put("whlog",selfDefineConfigWhLog);
        map.put("superadmin", selfDefineConfigRbacSuperAdmin);
        return ApiResponse.builder().data(map).build();

    }

    @PostConstruct
    public void init(){
        resetWhValue();
        resetSuperAdminConfig();
    }

    private void resetWhValue(){
        RedisKeyBuilder rkb= WhatelyCoreCacheKeyEnum.CORE_CONFIG_LOG_ASPECT_OPEN.obtainKeyBuilder();
        String value = redisCacheStringUtils.get(rkb);
        boolean openFlag = true;
        if(value!=null){
            openFlag = Boolean.valueOf(value);
        }
        selfDefineConfigWhLog.setOpen(openFlag);

        rkb= WhatelyCoreCacheKeyEnum.CORE_CONFIG_LOG_ASPECT_PRINT_PARAM.obtainKeyBuilder();
        value = redisCacheStringUtils.get(rkb);
        boolean printParamFlag = false;
        if(value!=null){
            printParamFlag = Boolean.valueOf(value);
        }
        selfDefineConfigWhLog.setOpenPrintParam(printParamFlag);
    }

    private void resetSuperAdminConfig(){
        RedisKeyBuilder rkb= WhatelyCoreCacheKeyEnum.CORE_CONFIG_RBAC_SUPER_ADMIN.obtainKeyBuilder();
        String superAdminConfigValue = redisCacheStringUtils.get(rkb);

        if(StringUtils.isNotBlank(superAdminConfigValue)){
            if(superAdminConfigValue.equals("true")){
                selfDefineConfigRbacSuperAdmin.setEnable(true);
            }else if(superAdminConfigValue.startsWith("1-")){
                selfDefineConfigRbacSuperAdmin.setEnable(true);
                //去掉1-
                selfDefineConfigRbacSuperAdmin.setWhite(superAdminConfigValue.substring(2));
            }else{
                selfDefineConfigRbacSuperAdmin.setEnable(false);
            }
        }
    }

}
